package com.haiyi.software.app.basic;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.support.v4.view.ViewPager.LayoutParams;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.haiyi.software.app.ActivityModifyInformation;
import com.haiyi.software.app.ActivityRecharge;
import com.haiyi.software.app.ActivityReport;
import com.haiyi.software.app.ActivityMine;
import com.haiyi.software.app.LoginActivity;
import com.haiyi.software.app.MainApplication;
import com.haiyi.software.app.R;
import com.haiyi.software.tools.L;

public abstract class ActivityPersonalBasicMenu extends
		ActivityPersonalBasicFollow {

	private final static String TAG = "ActivityPersonalBasicMenu";

	private PopupWindow popupWindow;

	private View view;

	private ListView lv_group;

	private List<Map<String, String>> groups;

	private String[] ary = { "举报该用户" };

	private Button button;

	protected void showMenu(Context context, View v) {

		button = (Button) v;

		WindowManager windowManager = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		if (popupWindow == null) {
			LayoutInflater layoutInflater = (LayoutInflater) context
					.getSystemService(context.LAYOUT_INFLATER_SERVICE);

			view = layoutInflater.inflate(R.layout.popup_menu, null);

			lv_group = (ListView) view.findViewById(R.id.list_menu);

			// 加载数据
			groups = new ArrayList<Map<String, String>>();

			for (String item : ary) {
				Map<String, String> map = new HashMap<String, String>();
				map.put("textView", item);
				groups.add(map);
			}

			String[] from = { "textView" };

			int[] to = { R.id.textView_menu };

			SimpleAdapter adpt = new SimpleAdapter(context, groups,
					R.layout.item_menu, from, to);

			lv_group.setAdapter(adpt);

			popupWindow = new PopupWindow(view, LayoutParams.MATCH_PARENT,
					LayoutParams.WRAP_CONTENT);
		}

		// 使其聚集
		popupWindow.setFocusable(true);
		// 设置允许在外点击消失
		popupWindow.setOutsideTouchable(true);
		// 这个是为了点击“返回Back”也能使其消失，并且并不会影响你的背景
		popupWindow.setBackgroundDrawable(new BitmapDrawable());

		// 显示的位置为:屏幕的宽度的一半-PopupWindow的高度的一半

		// int xPos = windowManager.getDefaultDisplay().getWidth() / 2
		//
		// - popupWindow.getWidth() / 2;
		//
		// Log.i("coder", "windowManager.getDefaultDisplay().getWidth()/2:"
		//
		// + windowManager.getDefaultDisplay().getWidth() / 2);

		//

		// Log.i("coder", "popupWindow.getWidth()/2:" + popupWindow.getWidth() /
		// 2);

		// Log.i("coder", "xPos:" + xPos);

		popupWindow.showAsDropDown(button);

		lv_group.setOnItemClickListener(itemClick);
	}

	private OnItemClickListener itemClick = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			// TODO 自动生成的方法存根
			TextView txtvView = (TextView) view;

			Log.i(TAG, txtvView.getText().toString());

			if (txtvView.getText().toString().equals("举报该用户")) {

				Intent intent = new Intent();
				intent.setClass(view.getContext(), ActivityReport.class);

				if (button.getTag() != null) {

					Map<String, Object> mapUser = (Map<String, Object>) button
							.getTag();

					L.i("BTNTAG", button.getTag().toString());

					intent.putExtra("reportedUID",
							mapUser.get("uid") != null ? mapUser.get("uid")
									.toString() : "");
					intent.putExtra("reportedName",
							mapUser.get("uname") != null ? mapUser.get("uname")
									.toString() : "");
					intent.putExtra(
							"reportedUsername",
							mapUser.get("username") != null ? mapUser.get(
									"username").toString() : "");

					startActivity(intent);
				} else {
					Toast.makeText(ActivityPersonalBasicMenu.this, "网络连接错误",
							Toast.LENGTH_LONG).show();
				}
			}

			popupWindow.dismiss();

		}
	};

}
